package com.xh.web.controller;


import cn.hutool.core.io.FileUtil;
import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.DGenTable;
import com.xh.web.domain.DGenTableColumn;
import com.xh.web.service.IDTableService;
import com.xh.web.service.IDbGenTableColumnService;
import com.xh.web.service.IDbGenTableService;
import com.xh.web.util.StringUtils;
import com.xh.web.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成 操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("db/gen")
public class DbGenController extends BaseController
{
    @Autowired
    private IDbGenTableService genTableService;

    @Autowired
    private IDbGenTableColumnService genTableColumnService;
    @Autowired
    private IDTableService dTableService;


    /**
     * 查询代码生成列表
     */
    @GetMapping("/list")
    public TableDataInfo genList(DGenTable genTable)
    {
        genTableService.deleteGenTableById(genTable.getTableId());
        genTableService.importGenTable(genTable);
        List<DGenTable> list = genTableService.selectGenTableList(genTable);
        return getDataTable(list);
    }

    /**
     * 再次查询代码生成列表
     */
    @GetMapping("/selectList")
    public TableDataInfo selectByCode(DGenTable genTable) {
        List<DGenTable> list = genTableService.selectGenTableList(genTable);
        return getDataTable(list);
    }


    /**
     * 修改代码生成业务
     */
    @GetMapping(value = "/{talbleId}")
    public AjaxResult getInfo(@PathVariable String talbleId)
    {
        DGenTable table = genTableService.selectGenTableById(talbleId);
        List<DGenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("info", table);
        map.put("rows", list);
        return AjaxResult.success(map);
    }

    /**
     * 查询数据表字段列表
     */
    @GetMapping("/column/{tableId}")
    public TableDataInfo columnList(@PathVariable("tableId") String tableId)
    {
        TableDataInfo dataInfo = new TableDataInfo();
        List<DGenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
        dataInfo.setRows(list);
        dataInfo.setTotal(list.size());
        return dataInfo;
    }


    /**
     * 修改保存代码生成业务
     */
    @PutMapping("/updateTable")
    public AjaxResult editSave(@RequestBody DGenTable genTable)
    {
        genTableService.updateGenTable(genTable);
        return AjaxResult.success();
    }

    /**
     * 预览代码
     */
    @GetMapping("/preview/{tableId}")
    public AjaxResult preview(@PathVariable("tableId") String tableId) throws IOException
    {
        Map<String, String> dataMap = genTableService.previewCode(tableId);
        return AjaxResult.success(dataMap);
    }

    /**
     * 生成代码（下载方式）
     */
    @GetMapping("/download/{tableName}")
    public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
    {
        byte[] data = genTableService.downloadCode(tableName);
        //genCode(response, data);
    }

    /**
     * 生成代码（自定义路径）
     */
    @GetMapping("/genCode/{tableId}")
    public AjaxResult genCode(HttpServletResponse response, @PathVariable("tableId") String tableId)
    {
        genTableService.generatorCode(tableId);
        return AjaxResult.success();
    }

    /**
     * 批量生成代码
     */
    @GetMapping("/batchGenCode")
    public AjaxResult batchGenCode(String tableId,String filePath) throws IOException
    {
        if(StringUtils.isEmpty(filePath)){
            return AjaxResult.error("目录不能为空");
        }
        if(!FileUtil.isDirectory(filePath)){
            return AjaxResult.error("目录不正确");
        }
        byte[] data = genTableService.downloadCode(tableId);
        String fileName=filePath+"/tdmodel.zip";
        FileUtil.writeBytes(data,new File(fileName));
        return AjaxResult.success();
    }


    /*private void genCode( byte[] data) throws IOException
    {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"teamdesigner.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        //response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }*/
}
